package com.horoscope.astrology.zodiac.signs.free.adapter.quize

import android.util.SparseArray
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.fragment.quiz.QuizImageFragment
import com.horoscope.astrology.zodiac.signs.free.fragment.quiz.QuizTextFragment

class QuizPagerAdapter(fm: FragmentManager, private val mBean: QuizBean) :
    FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {


    private val sparseArray = SparseArray<Fragment>()
    private var mFragment: Fragment? = null
    override fun getItem(position: Int): Fragment {
        return if (sparseArray[position] == null) {
            mFragment = if (mBean.questions[position].type == "text") {
                QuizTextFragment.getFragment(position, mBean)
            } else {
                QuizImageFragment.getFragment(position, mBean)
            }
            sparseArray.put(position, mFragment)
            mFragment!!
        } else {
            sparseArray[position]
        }
    }

    override fun getCount(): Int {

        return mBean.questions.size
    }


    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {

    }

    override fun getPageTitle(position: Int): CharSequence? {
        return "1"
    }
}